package com.world.business.tenbatsu.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.world.business.tenbatsu.domain.Tenbatsu;
import com.world.business.tenbatsu.domain.query.TenbatsuAddRequest;
import com.world.business.tenbatsu.domain.query.TenbatsuPageRequest;
import com.world.business.tenbatsu.domain.query.TenbatsuUpdateRequest;
import com.world.business.tenbatsu.domain.vo.TenbatsuVO;
import com.world.business.tenbatsu.mapper.TenbatsuMapper;
import com.world.business.tenbatsu.service.ITenbatsuService;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.exception.BusinessException;
import com.world.common.security.SecurityUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 天谴 服务实现类
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-31
 */
@Service
public class TenbatsuServiceImpl extends ServiceImpl<TenbatsuMapper, Tenbatsu> implements ITenbatsuService {

    @Override
    public Long add(TenbatsuAddRequest request) {
        LambdaQueryWrapper<Tenbatsu> queryWrapper = Wrappers.lambdaQuery(Tenbatsu.class);
        queryWrapper.eq(Tenbatsu::getRealmId, request.getRealmId());
        queryWrapper.eq(Tenbatsu::getDelFlag, DelFlagEnum.NORMAL.getCode());
        Tenbatsu one = this.baseMapper.selectOne(queryWrapper);
        if (BeanUtil.isNotEmpty(one)) {
            throw new BusinessException("境界对应的数据已存在！");
        }
        Tenbatsu tenbatsu = BeanUtil.copyProperties(request, Tenbatsu.class);

        tenbatsu.setCreateBy(SecurityUtils.getLoginUser().getName());
        tenbatsu.setCreateById(SecurityUtils.getLoginUser().getId());
        tenbatsu.setCreateTime(LocalDateTime.now());

        this.baseMapper.insert(tenbatsu);
        return tenbatsu.getId();
    }

    @Override
    public void update(TenbatsuUpdateRequest request) {
        LambdaQueryWrapper<Tenbatsu> queryWrapper = Wrappers.lambdaQuery(Tenbatsu.class);
        queryWrapper.eq(Tenbatsu::getId, request.getId());
        queryWrapper.eq(Tenbatsu::getDelFlag, DelFlagEnum.NORMAL.getCode());
        Tenbatsu tenbatsu = this.baseMapper.selectOne(queryWrapper);
        if (BeanUtil.isEmpty(tenbatsu)) {
            throw new BusinessException("数据不存在！");
        }

        LambdaUpdateWrapper<Tenbatsu> updateWrapper = Wrappers.lambdaUpdate(Tenbatsu.class);
        updateWrapper.eq(Tenbatsu::getId, request.getId());
        updateWrapper.set(StrUtil.isNotBlank(request.getName()), Tenbatsu::getName, request.getName());
        updateWrapper.set(request.getStartValue() != null, Tenbatsu::getStartValue, request.getStartValue());
        updateWrapper.set(request.getEndValue() != null, Tenbatsu::getEndValue, request.getEndValue());
        updateWrapper.set(StrUtil.isNotBlank(request.getStatus()), Tenbatsu::getStatus, request.getStatus());
        updateWrapper.set(StrUtil.isNotBlank(request.getRemark()), Tenbatsu::getRemark, request.getRemark());

        updateWrapper.set(Tenbatsu::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Tenbatsu::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Tenbatsu::getUpdateTime, LocalDateTime.now());

        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public void delete(Long id) {
        LambdaUpdateWrapper<Tenbatsu> updateWrapper = Wrappers.lambdaUpdate(Tenbatsu.class);
        updateWrapper.eq(Tenbatsu::getId, id);
        updateWrapper.set(Tenbatsu::getDelFlag, DelFlagEnum.DELETE.getCode());

        updateWrapper.set(Tenbatsu::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Tenbatsu::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Tenbatsu::getUpdateTime, LocalDateTime.now());

        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public IPage<TenbatsuVO> getPage(IPage<Tenbatsu> page, TenbatsuPageRequest request) {
        return this.baseMapper.getPage(page, request);
    }
}
